package com.creating.mict.common.mess;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.creating.mict.common.DateUtils;
import com.creating.mict.common.db.OperOrcl;

public class SMSCaClient {
	private static Logger logger = Logger.getLogger(SMSCaClient.class);

	private static String urlStr = "http://114.113.149.115/sp/ifws/cpsms.asmx";
	private static String sn1 = "60006";
	private static String sn2 = "ert4321";
	private static String sn3 = "105";

	/**
	 * 
	 * @Title: sendMessXmlInfo
	 * @Description: 发送短信POST请求的XML格式
	 * @param
	 * @return
	 * @return String 返回类型
	 * @throws
	 */
	public static String sendMessXmlInfo(String telNum, String content) {
		StringBuilder sb = new StringBuilder();
		sb.append("<?xml version = \"1.0\" encoding=\"utf-8\"?>\n");
		sb
				.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ");
		sb.append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ");
		sb
				.append(" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n");

		sb.append("<soap:Body>\n");
		sb.append("<sendmt xmlns=\"http://cpsms.org.cn/\">\n");
		sb.append("<sn1>" + sn1 + "</sn1>\n");
		sb.append("<sn2>" + sn2 + "</sn2>\n");
		sb.append("<sn3>" + sn3 + "</sn3>\n");
		sb.append("<sn4></sn4>\n");
		sb.append("<sn5>" + telNum + "</sn5>\n");
		sb.append("<sn6>" + content + "</sn6>\n");
		sb.append("<sn7>0</sn7>\n");
		sb.append("<sn8></sn8>\n");
		sb.append("<sn9>0</sn9>\n");
		sb.append("<sn10></sn10>\n");
		sb.append("</sendmt>\n");

		sb.append("</soap:Body>\n");
		sb.append("</soap:Envelope>\n");

		return sb.toString();
	}

	/**
	 * 
	 * @Title: receiveMessXmlInfo
	 * @Description: 获取回复短信POST的XML格式
	 * @param
	 * @return
	 * @return String 返回类型
	 * @throws
	 */
	public static String receiveMessXmlInfo() {
		StringBuilder sb = new StringBuilder();
		sb.append("<?xml version = \"1.0\" encoding=\"utf-8\"?>\n");
		sb
				.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ");
		sb.append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ");
		sb
				.append(" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n");

		sb.append("<soap:Body>\n");
		sb.append("<recvmo xmlns=\"http://cpsms.org.cn/\">\n");
		sb.append("<sn1>" + sn1 + "</sn1>\n");
		sb.append("<sn2>" + sn2 + "</sn2>\n");
		sb.append("<sn3>" + sn3 + "</sn3>\n");
		sb.append("</recvmo>\n");

		sb.append("</soap:Body>\n");
		sb.append("</soap:Envelope>\n");

		return sb.toString();
	}

	/**
	 * 
	 * @Title: queryCountXmlInfo
	 * @Description: 获取短信剩余条数POST的XML格式
	 * @param
	 * @return
	 * @return String 返回类型
	 * @throws
	 */
	public static String queryCountXmlInfo() {
		StringBuilder sb = new StringBuilder();
		sb.append("<?xml version = \"1.0\" encoding=\"utf-8\"?>\n");
		sb
				.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ");
		sb.append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ");
		sb
				.append(" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n");

		sb.append("<soap:Body>\n");
		sb.append("<querycount xmlns=\"http://cpsms.org.cn/\">\n");
		sb.append("<sn1>" + sn1 + "</sn1>\n");
		sb.append("<sn2>" + sn2 + "</sn2>\n");
		sb.append("<sn3>" + sn3 + "</sn3>\n");
		sb.append("</querycount>\n");

		sb.append("</soap:Body>\n");
		sb.append("</soap:Envelope>\n");

		return sb.toString();
	}

	/**
	 * 
	 * @Title: testPost
	 * @Description: POST请求，返回XML信息
	 * @param
	 * @param urlStr
	 * @param
	 * @param xmlInfo
	 * @return void 返回类型
	 * @throws
	 */
	public static String testPost(String xmlInfo) {
		String xmlReturn = "";
		try {
			URL url = new URL(urlStr);
			HttpURLConnection con = (HttpURLConnection) url.openConnection();
			con.setDoOutput(true);
			con.setDoInput(true);
			con.setUseCaches(false);

			con.setRequestMethod("POST");
			con.setRequestProperty("User-Agent", "sp-client");
			con.setRequestProperty("Content-Length", Integer.toString(xmlInfo
					.length()));
			con.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
			OutputStreamWriter out = new OutputStreamWriter(con
					.getOutputStream(), "utf-8");

			out.write(new String(xmlInfo.getBytes()));
			out.flush();
			out.close();
			BufferedReader br = new BufferedReader(new InputStreamReader(con
					.getInputStream(), "utf-8"));
			String line = "";

			for (line = br.readLine(); line != null; line = br.readLine()) {
				xmlReturn += line;
			}
			logger.info("返回结果1：" + xmlReturn);
//			xmlReturn = new String(xmlReturn.getBytes(), "utf-8");
//			logger.info("返回结果2：" + xmlReturn);

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return xmlReturn;
	}

	/**
	 * 
	* @Title: saveMessSendRecord 
	* @Description: 保存短信发送记录
	* @param @param fcontent
	* @param @param fsender
	* @param @param fphone
	* @param @param ftype 
	* @return void    返回类型 
	* @throws
	 */
	public void saveMessSendRecord(String fcontent, String fsender, String fphone, String ftype) {
		try {
			OperOrcl operOrcl = new OperOrcl();
			DateUtils dateUtils = new DateUtils();
			String sql = "insert into TDS_MESS_SEND(fid, fdate, fcontent, fsender, fphone, ftype, fstate) "
				+ " values('"
				+ dateUtils.getTransactionIdByLongTime()
				+ "',"
				+ " to_date('"
				+ dateUtils.getCurrentDateTime()
				+ "','YYYY-MM-DD HH24:MI:SS'), "
				+ " '"
				+ fcontent
				+ "', "
				+ " '"
				+ fsender
				+ "', "
				+ " '"
				+ fphone
				+ "', "
				+ " '"
				+ ftype
				+ "')";
	
		operOrcl.getUpdate(sql);
		logger.info("保存短信发送记录:" + fphone + fcontent);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}

	public static void main(String[] args) {
		 testPost(sendMessXmlInfo("18001218516", "高压：160mmHg低压：96mmHg脉率：63min10-0404-06-2013"));
//		 System.out.println(testPost(receiveMessXmlInfo()));
//		 System.out.println(testPost(queryCountXmlInfo()));
		 
//		try {
//			String content = new String(
//					"楂樺帇锛?60mmHg浣庡帇锛?6mmHg鑴夌巼锛?3min10-0401-06-2012".getBytes(),
//					"utf-8");
//			System.out.println("----" + content);
//			
//			content = new String(
//					"楂樺帇锛?60mmHg浣庡帇锛?6mmHg鑴夌巼锛?3min10-0401-06-2012"
//							.getBytes("ISO8859_1"), "GBK");
//			System.out.println("----" + content);
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}
}